package com.aspirecn.core.datacache;

import java.io.File;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.math.RandomUtils;
import org.apache.log4j.Logger;

import com.aspirecn.core.commons.Configuration;
import com.aspirecn.core.commons.FileUtils;
import com.aspirecn.core.object.ImageQuestion;
import com.csvreader.CsvReader;

/**
 * 图片问题缓存
 * 
 * @author wangxinning
 * 
 */
public class ImageQuestionCache extends BaseLocalCache {

	public final static Logger logger = Logger
			.getLogger(ImageQuestionCache.class);

	private List<ImageQuestion> imageQuestionList = new ArrayList<ImageQuestion>();

	private static ImageQuestionCache instance = new ImageQuestionCache();

	private ImageQuestionCache() {

		this.dataFile = new File(Configuration.getInstance()
				.getDownloadFilePath()
				+ File.separator
				+ "imagevalidate"
				+ File.separator + "blob.csv");
		load();
	}

	public static ImageQuestionCache getInstance() {
		return instance;
	}

	public void load() {

		if (!loading.getAndSet(true) && this.dataFile.exists()) {

			long start = System.currentTimeMillis();

			try {

				CsvReader reader = new CsvReader(this.dataFile.toString(), ',',
						Charset.forName("gbk"));

				while (reader.readRecord()) {

					ImageQuestion imageQuestion = new ImageQuestion();
					imageQuestion.setYesResult(reader.get(1));
					imageQuestion.setNoResult(reader.get(2));

					File imageFile = new File(Configuration.getInstance()
							.getDownloadFilePath()
							+ File.separator
							+ "imagevalidate"
							+ File.separator
							+ reader.get(3)
							+ ".jpg");

					imageQuestion.setFilename(imageFile.toString());

					if (imageFile.exists()) {
						imageQuestionList.add(imageQuestion);
					}
				}

				reader.close();

			} catch (Exception e) {
				logger.error("图片问题信息加载异常", e);
			}

			loading.set(false);
			long end = System.currentTimeMillis();
			updateFileDownloadTime();
			logger.info("图片问题信息加载完成，time=" + (end - start) + "ms,size="
					+ imageQuestionList.size());

		} else {
			loading.set(false);
			logger.info("配置关闭，图片问题信息未加载");
		}

		loading.set(false);
	}

	@Override
	public void clear() {
		imageQuestionList.clear();
	}

	@Override
	public void reload() {

		if (this.fileIsChange()) {
			this.load();
		} else {
			logger.info("没变化，图片问题信息未加载");
		}
	}
	
	public ImageQuestion getRandom()
	{
		int count = imageQuestionList.size();
		
		if( this.isLoading() || count == 0 )
		{
			return null;
		}
		else
		{
			return imageQuestionList.get(RandomUtils.nextInt(count));
		}		
	}	

	public static void main(String[] args) {
		ImageQuestionCache geoCache = ImageQuestionCache.getInstance();
		System.out.println(geoCache.getRandom());
		System.out.println(geoCache.getRandom());
		System.out.println(geoCache.getRandom());
		System.out.println(geoCache.getRandom());
		System.out.println(geoCache.getRandom());
	}
}